package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"log"
	"os"
	"struct_tag/version"
)

var author string

var rootCmd = &cobra.Command{
	Use:   "cobra [string to action]",
	Short: "cobra command tool",
	Long:  "cobra is a simple cli command tool",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		log.Println("1. rootCmd PersistentPreRun")
	},
	PreRun: func(cmd *cobra.Command, args []string) {
		log.Println("2. rootCmd PreRun")
		//setup()
	},
	Run: func(cmd *cobra.Command, args []string) {
		log.Println("3. rootCmd Run, auth:" + author)
		run()
	},
	PostRun: func(cmd *cobra.Command, args []string) {
		log.Println("4. rootCmd PostRun")
	},
	PersistentPostRun: func(cmd *cobra.Command, args []string) {
		log.Println("5. rootCmd PersistentPostRun")
	},
}

func init() {
	rootCmd.PersistentFlags().StringVar(&author, "author", "CKeen", "Author name for copyright attribution")
	rootCmd.AddCommand(version.VersionCmd)
}

func run() {
	fmt.Println("go root cmd run")
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		_, err := fmt.Fprintln(os.Stderr, err)
		if err != nil {
			return
		}
		os.Exit(1)
	}
}
